package com.ysd.lis.service.qi.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.qi.QiIndexFeeitemSign;
import com.ysd.lis.entity.qi.QiIndexPatType;
import com.ysd.lis.mapper.qi.QiIndexPatTypeMapper;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.qi.QiIndexPatTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-08-08
 */
@Service
public class QiIndexPatTypeServiceImpl extends ServiceImpl<QiIndexPatTypeMapper, QiIndexPatType> implements QiIndexPatTypeService {

    @Autowired
    protected QiIndexPatTypeMapper qiIndexPatTypeMapper;
    @Autowired
    SysBasDictDetailService sysBasDictDetailService;

    @Override
    public Result getPatTypeList() {
        List<Map<String, Object>> patTypeList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("PATTYPE").getData();
        List<QiIndexPatType> qiIndexPatTypes = qiIndexPatTypeMapper.selectList(new LambdaQueryWrapper<>());
        for (Map<String, Object> map : patTypeList) {
            Optional<QiIndexPatType> code = qiIndexPatTypes.stream().filter(item -> item.getPatType().equals(map.get("code"))).findFirst();
            if (code.isPresent()){
                map.put("qiType", code.get().getQiType());
            }
        }
        return Result.succ(patTypeList);
    }

    @Override
    public Result addPatType(List<QiIndexPatType> qiIndexPatType) {
        qiIndexPatTypeMapper.delete(new LambdaQueryWrapper<QiIndexPatType>());
        this.saveBatch(qiIndexPatType);
        return Result.succ("保存成功!");
    }
}
